﻿using System.Collections.Generic;
using System.Linq;
using UcAsp.Net.Model;

namespace UcAsp.Net.MSSQLDAL
{
    public class SetFiledDAL : UcAsp.Net.IDAL.ISetFiledDAL
    {
        public List<SetFiled> Select(int categoriesid)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                var query = entities.SetFileds.Where(o => o.CategoreiesID == categoriesid).OrderBy(o=>o.Rank);
                return query.ToList();
            }
        }

        public SetFiled Limit(int id)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                return entities.SetFileds.FirstOrDefault(o => o.FiledID == id);
            }
        }

        public bool Insert(SetFiled model)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                try
                {
                    entities.SetFileds.Add(model);
                    entities.SaveChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public bool Update(SetFiled model)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                try
                {
                    System.Data.Entity.Infrastructure.DbEntityEntry<SetFiled> entry = entities.Entry<SetFiled>(model);
                    entry.State = System.Data.EntityState.Modified;
                    entities.SaveChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        public bool Delete(SetFiled model)
        {
            using (UcAspNetEntities entities = new UcAspNetEntities())
            {
                System.Data.Entity.Infrastructure.DbEntityEntry<SetFiled> entry = entities.Entry<SetFiled>(model);
                entry.State = System.Data.EntityState.Deleted;
                entities.SaveChanges();
                return true;
            }
        }
    }
}